package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    
    //constructor which does nothing
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
       
    }
    
    public void start()
    {
       
        addressMenu();
    }
    
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          if(choice == 1)
          {   //show person
              System.out.println("\f *** Person information ***");
              getPerson();
              Scanner keyboard = new Scanner(System.in);
              System.out.println("\nType 'Next' to continue");
              String next = keyboard.next();
          }
          else{
              if(choice == 2){
              Scanner keyboard =  new Scanner(System.in);
              System.out.println("\f *** New Person ***");
              System.out.println("Please Enter the details for the new person");
              long id = inputPersonsID();
              String name = inputName();
              String address = inputAddress();
              String postalCode = inputPostalCode();
              String city = inputCity();
              String phone = inputPhone();
              addrCtr.createPerson(id,name,address,postalCode,city,phone);
              System.out.println("Please type 'Next' to continue");
              String next = keyboard.next();
                //add person
              }  
              else{
                  if(choice == 3){
                      addrCtr.listAllPersons();
                      System.out.print("\n");
                      long id = inputPersonsID();
                      addrCtr.deletePerson(id);
                      System.out.println("Please type 'Next' to continue");
                      Scanner keyboard = new Scanner(System.in);
                      String next = keyboard.next();
                      // delete person
                  }  
                  else{
                      if(choice == 4){
                          Scanner keyboard = new Scanner(System.in);
                          System.out.println("\f *** Update Person ***");
                          addrCtr.listAllPersons();
                          System.out.println("Please enter the ID of the person you want to update");
                          System.out.print("> ");
                          long id = keyboard.nextInt();
                          System.out.println("Please enter the choise that corresponds to the information you want to update");
                          System.out.println("(1) Name");
                          System.out.println("(2) Adress");
                          System.out.println("(3) Postal Code");
                          System.out.println("(4) City");
                          System.out.println("(5) Phone Number");
                          System.out.print("> ");
                          int updateChoice = keyboard.nextInt();
                          updatePerson(id,updateChoice);
                          System.out.println("Please type 'Next' to continue");
                          String next = keyboard.next();
                          // update person
                      }
                      else{
                          if(choice == 5){
                              Scanner keyboard = new Scanner(System.in);
                              addrCtr.listAllPersons();
                              System.out.println("Type 'Next' to continue");
                              String next = keyboard.next();
                              
                            }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    private void updatePerson(long id, int choice)
    {
        Scanner keyboard = new Scanner(System.in);
        if (choice==1)
        {
            System.out.println("Please enter the new Name");
            String name = keyboard.next();
            addrCtr.updatePersonName(id,name);
        }
        if (choice==2)
        {
            System.out.println("Please enter the new Address");
            String address = keyboard.next();
            addrCtr.updatePersonAddress(id,address);
        }
        if (choice==3)
        {
            System.out.println("Please enter the new Postal Code");
            String postalcode = keyboard.next();
            addrCtr.updatePersonPostalCode(id,postalcode);
        }
        if (choice==4)
        {
            System.out.println("Please enter the new City");
            String city = keyboard.next();
            addrCtr.updatePersonCity(id,city);
        }
        if (choice==5)
        {
            System.out.println("Please enter the new Phone Number");
            String phone = keyboard.next();
            addrCtr.updatePersonPhone(id,phone);
        }
    }
    
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
        }
        
    //the method reads id for a person and return
    // 
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(1) ID:  ");
           try{
               
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return id;
    }
    
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.print("(2) Name:  ");
         String name = keyboard.next();
         return name;
    }
    
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.print("(2) Address: ");
        String address = keyboard.next();
        return address;
    }
   
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.print("(3) Postal Code:  ");
         String code = keyboard.next();
         return code;
    }
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.print("(4) City:  ");
         String city = keyboard.next();
         return city;
    } 
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.print("(5) Phone Number:  ");
         String phone = keyboard.next();
         return phone;
    } 
    
    
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
    public void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }

    private void getPerson()
    {//is to be changed when the address book controller is completed
        long id = inputPersonsID();
        addrCtr.getPerson(id);
       
    }   
    
    public AddressCtr getAdrCtr()
    {
        return addrCtr;
    }
}
            
        
